package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.entity.FreshNewsListBean;
import com.beiing.tianshuai.tianshuai.util.DisplayUtil;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 新鲜事列表适配器
 *
 * @author LZQ
 * @date 2017/7/17
 */
public class FreshNewsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements View.OnClickListener {

    /**
     * 新鲜事列表
     */
    private static final int TYPE_NORMAL = 1;

    /**
     * 多张封面图
     */
    private static final int TYPE_THREE = 2;


    /**
     * 图片名称分隔符
     */
    private static final String SPLITTER = ",";

    private List<FreshNewsListBean.DataBean.FreshBean> newsData;
    private Context mContext;
    private OnItemClickListener mOnItemClickListener;
    private OnItemViewClickListener mOnItemViewClickListener;

    /**
     * 红色和灰色点赞状态的drawable
     */
    private Drawable mRedPraise, mGrayPraise;

    private RequestOptions coverOptions;

    public FreshNewsRecyclerViewAdapter(List<FreshNewsListBean.DataBean.FreshBean> newsData, Context context) {
        this.newsData = newsData;
        this.mContext = context;
        mRedPraise = ContextCompat.getDrawable(mContext, R.drawable.ic_dynamic_praise_red);
        mGrayPraise = ContextCompat.getDrawable(mContext, R.drawable.ic_dynamic_praise);
        coverOptions = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .override(600, 300);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 1) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_discovery_onepic, parent, false);
            return new SinglePicViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_discovery_threepic, parent, false);
            return new TriplePicViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        //新鲜事列表部分
        String covers = newsData.get(position).getCover();
        String[] coversAll = null;
        if (covers.contains(SPLITTER)) {
            //分割为数组
            coversAll = covers.split(SPLITTER);
        }
        final FreshNewsListBean.DataBean.FreshBean bean = newsData.get(position);
        if (covers.contains(SPLITTER) && coversAll != null && coversAll.length >= 3) {
            final TriplePicViewHolder triplePicViewHolder = (TriplePicViewHolder) holder;
            triplePicViewHolder.discoveryTitle.setText(newsData.get(position).getTitle());
            triplePicViewHolder.discoveryUserName.setText(newsData.get(position).getName());

            //************** 设置16:9封面图 *****************
            //屏幕宽度像素
            float screenWidth = DisplayUtil.getMobileWidth(mContext);
            ViewGroup.LayoutParams layoutParams = triplePicViewHolder.discoveryCover1.getLayoutParams();
            layoutParams.width = (int) ((screenWidth - DisplayUtil.dip2px(mContext, 30)) / 3);
            layoutParams.height = (int) (layoutParams.width / 1.78);
            triplePicViewHolder.discoveryCover1.setLayoutParams(layoutParams);
            ViewGroup.LayoutParams layoutParams1 = triplePicViewHolder.discoveryCover2.getLayoutParams();
            layoutParams1.width = (int) ((screenWidth - DisplayUtil.dip2px(mContext, 30)) / 3);
            layoutParams1.height = (int) (layoutParams.width / 1.78);
            triplePicViewHolder.discoveryCover2.setLayoutParams(layoutParams1);
            ViewGroup.LayoutParams layoutParams2 = triplePicViewHolder.discoveryCover3.getLayoutParams();
            layoutParams2.width = (int) ((screenWidth - DisplayUtil.dip2px(mContext, 30)) / 3);
            layoutParams2.height = (int) (layoutParams.width / 1.78);
            triplePicViewHolder.discoveryCover3.setLayoutParams(layoutParams2);

            Glide.with(mContext.getApplicationContext()).load(HttpRequestConstant.FRESH_COVER_URL + coversAll[0])
                    .apply(coverOptions)
                    .into(triplePicViewHolder.discoveryCover1);
            Glide.with(mContext.getApplicationContext()).load(HttpRequestConstant.FRESH_COVER_URL + coversAll[1])
                    .apply(coverOptions)
                    .into(triplePicViewHolder.discoveryCover2);
            Glide.with(mContext.getApplicationContext()).load(HttpRequestConstant.FRESH_COVER_URL + coversAll[2])
                    .apply(coverOptions)
                    .into(triplePicViewHolder.discoveryCover3);
            String avatar = newsData.get(position).getPhoto();
            Glide.with(mContext.getApplicationContext()).load(HttpRequestConstant.USER_PIC_HEAD
                    + (TextUtils.isEmpty(avatar) ? "1.jpg" : avatar))
                    .into(triplePicViewHolder.discoveryUserHeader);
            triplePicViewHolder.discoveryLike.setText(newsData.get(position).getPraise());
            triplePicViewHolder.discoveryCheck.setText(newsData.get(position).getView());
            //1为已点赞
            if (newsData.get(position).getIs_thing() == 1) {
                triplePicViewHolder.discoveryLike.setCompoundDrawablesWithIntrinsicBounds(mRedPraise, null, null, null);
            } else {
                triplePicViewHolder.discoveryLike.setCompoundDrawablesWithIntrinsicBounds(mGrayPraise, null, null, null);
            }
            triplePicViewHolder.itemView.setTag(position);
            triplePicViewHolder.itemView.setOnClickListener(this);
            triplePicViewHolder.discoveryLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemViewClickListener.onLikeClick(view, holder.getAdapterPosition(), bean,
                            ((TriplePicViewHolder) holder).mTvAddOne);
                    int isThing = bean.getIs_thing();
                    if (isThing == 1) {
                        bean.setIs_thing(0);
                    } else {
                        bean.setIs_thing(1);
                    }
                }
            });
            triplePicViewHolder.llUserHeader.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemViewClickListener != null) {
                        mOnItemViewClickListener.onHeaderClick(view, triplePicViewHolder.getAdapterPosition(), bean);
                    }
                }
            });
        } else {
            final SinglePicViewHolder mHolder = (SinglePicViewHolder) holder;
            mHolder.tvTitle.setText(newsData.get(position).getTitle());
            mHolder.tvName.setText(newsData.get(position).getName());
            if (TextUtils.isEmpty(newsData.get(position).getCover())) {
                mHolder.ivCover.setVisibility(View.GONE);
            } else {
                mHolder.ivCover.setVisibility(View.VISIBLE);
                Glide.with(mContext.getApplicationContext()).load(HttpRequestConstant.FRESH_COVER_URL +
                        ((coversAll == null || coversAll.length == 0) ? newsData.get(position).getCover() : coversAll[0]))
                        .apply(coverOptions)
                        .into(mHolder.ivCover);
            }
            mHolder.itemView.setTag(position);
            mHolder.itemView.setOnClickListener(this);
            mHolder.tvLike.setText(newsData.get(position).getPraise());
            mHolder.tvCheckNum.setText(newsData.get(position).getView());
            String avatar = newsData.get(position).getPhoto();
            Glide.with(mContext.getApplicationContext()).load(HttpRequestConstant.USER_PIC_HEAD + (
                    TextUtils.isEmpty(avatar) ? "1.jpg" : avatar))
                    .into(mHolder.civHeader);
            //1为已点赞
            if (newsData.get(position).getIs_thing() == 1) {
                mHolder.tvLike.setCompoundDrawablesWithIntrinsicBounds(mRedPraise, null, null, null);
            } else {
                mHolder.tvLike.setCompoundDrawablesWithIntrinsicBounds(mGrayPraise, null, null, null);
            }

            mHolder.tvLike.setTag(position);
            mHolder.tvLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemViewClickListener.onLikeClick(view, holder.getAdapterPosition(), bean,
                            ((SinglePicViewHolder) holder).mTvAddOne);
                    int isThing = bean.getIs_thing();
                    if (isThing == 1) {
                        bean.setIs_thing(0);
                    } else {
                        bean.setIs_thing(1);
                    }
                }
            });
            mHolder.llUserHeader.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemViewClickListener != null) {
                        mOnItemViewClickListener.onHeaderClick(view, mHolder.getAdapterPosition(), bean);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return newsData != null ? newsData.size() : 0;
    }

    @Override
    public int getItemViewType(int position) {
        String cover = newsData.get(position).getCover();
        String[] coverAll = null;
        if (cover.contains(SPLITTER)) {
            //分割为数组
            coverAll = cover.split(SPLITTER);
        }
        if (null != coverAll && coverAll.length >= 3) {
            return TYPE_THREE;
        } else {
            return TYPE_NORMAL;
        }
    }

    @Override
    public void onClick(View view) {
        if (mOnItemViewClickListener != null) {
            mOnItemViewClickListener.onItemViewClick(view, (int) view.getTag());
        }
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(view, (int) view.getTag());
        }
    }

    /**
     * 新鲜事列表单张图片布局样式的ViewHolder
     */
    class SinglePicViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.discovery_title)
        TextView tvTitle;
        @BindView(R.id.discovery_header)
        CircleImageView civHeader;
        @BindView(R.id.discovery_name)
        TextView tvName;
        @BindView(R.id.discovery_like)
        TextView tvLike;
        @BindView(R.id.discovery_check)
        TextView tvCheckNum;
        @BindView(R.id.tv_add_one)
        TextView mTvAddOne;
        @BindView(R.id.discovery_cover)
        ImageView ivCover;
        @BindView(R.id.ll_user_header)
        LinearLayout llUserHeader;

        SinglePicViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 新鲜事列表三图布局样式的ViewHolder
     */
    class TriplePicViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.discovery_title)
        TextView discoveryTitle;
        @BindView(R.id.discovery_cover1)
        ImageView discoveryCover1;
        @BindView(R.id.discovery_cover2)
        ImageView discoveryCover2;
        @BindView(R.id.discovery_cover3)
        ImageView discoveryCover3;
        @BindView(R.id.discovery_user_header)
        CircleImageView discoveryUserHeader;
        @BindView(R.id.discovery_user_name)
        TextView discoveryUserName;
        @BindView(R.id.discovery_like)
        TextView discoveryLike;
        @BindView(R.id.tv_add_one)
        TextView mTvAddOne;
        @BindView(R.id.discovery_check)
        TextView discoveryCheck;
        @BindView(R.id.ll_user_header)
        LinearLayout llUserHeader;

        TriplePicViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 条目点击事件接口
     */
    public interface OnItemClickListener {

        /**
         * 条目点击事件
         *
         * @param view     用户所点击的视图
         * @param position 被点击条目的位置
         */
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    /**
     * 条目子View点击事件接口
     */
    public interface OnItemViewClickListener {

        /**
         * 条目点击事件
         *
         * @param view     用户所点击的视图
         * @param position 被点击条目所在位置
         */
        void onItemViewClick(View view, int position);

        /**
         * 点赞
         *
         * @param view     用户所点击的视图
         * @param position 被点击条目所在位置
         * @param bean     传递用户所点击条目的bean
         * @param tvAddOne +1视图
         */
        void onLikeClick(View view, int position, FreshNewsListBean.DataBean.FreshBean bean, TextView tvAddOne);

        /**
         * 用户头像的点击事件 跳转到用户的个人主页
         *
         * @param view     用户所点击的视图
         * @param position 被点击条目所在位置
         * @param bean     传递用户所点击条目的bean
         */
        void onHeaderClick(View view, int position, FreshNewsListBean.DataBean.FreshBean bean);
    }

    public void setOnItemViewClickListener(OnItemViewClickListener listener) {
        this.mOnItemViewClickListener = listener;
    }
}
